package com.itheima.contoller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.itheima.domain.Province;
import com.itheima.service.Impl.JLServiceImpl;
import com.itheima.service.JLService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @param
 * @return
 */
@WebServlet("/jlServlet")
public class JLServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        /*创建业务层对象*/
        JLService service = new JLServiceImpl();
        /*创建json类对象*/
        ObjectMapper om=new ObjectMapper();
        /*调用业务层方法，获取返回值*/
        List<Province> list = service.findAllProvince();
        //将返回值转换为json类型的字符串
        String s = om.writeValueAsString(list);
        System.out.println("s = " + s);
        //设置响应的字符集，重点是text/json
        resp.setContentType("text/json;charset=UTF-8");
        //写回
        resp.getWriter().write(s);
    }
}
